using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Security.Cryptography;

namespace AliCMS.SystemModule.Bll
{
    public class InstanceConfig : XmlObjectBase, IInstanceConfig
	{
		public InstanceConfig(int instanceID, string lang, bool checkRight, string database)
			: base(instanceID, lang, checkRight)
		{
            if (Manager.Channels == null)
                return;

            DataRow row = Manager.Channels.Select(Alias.Instance.InnerID + "=" + instanceID)[0];

            string dir = Manager.MapPath(Manager.GetDataPath(row[Alias.Instance.ID], Lang));
            Manager.MakeSureFolderExist(dir);
            string path = dir + "instance.config";
            if (!File.Exists(path))
            {
                XmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration></configuration>");               
                Save();
            }
            else
            {
                XmlDoc.Load(path);
            }
        }

		public override object ExecuteService(HttpContext context)
		{
            object result = null;

            string operation = context.Request.QueryString["Operation"];
            if (operation == "SetBasicSettings")
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                string value = null;
                value = context.Request.Form["Name"];
                if (value != null) item[Alias.InstanceConfig.Name] = value;
                value = context.Request.Form["Description"];
                if (value != null) item[Alias.InstanceConfig.Description] = value;
                value = context.Request.Form["Notice"];
                if (value != null) item[Alias.InstanceConfig.Notice] = value;
                value = context.Request.Form["Footer"];
                if (value != null) item[Alias.InstanceConfig.Footer] = value;
                value = context.Request.Form["StatCode"];
				if (value != null) item[Alias.InstanceConfig.StatCode] = value;
                value = context.Request.Form["UploadType"];
                if (value != null) item[Alias.InstanceConfig.UploadType] = value;
                value = context.Request.Form["Error404Redirect"];
                if (value != null) item[Alias.InstanceConfig.Error404Redirect] = value;
                value = context.Request.Form["ErrorRedirect"];
                if (value != null) item[Alias.InstanceConfig.ErrorRedirect] = value;
                value = context.Request.Form["DisplayCheckCode"];
                if (value != null) item[Alias.InstanceConfig.DisplayCheckCode] = value;
                value = context.Request.Form["Logo"];
                if (value != null) item[Alias.InstanceConfig.Logo] = value;
                value = context.Request.Form["Link"];
                if (value != null) item[Alias.InstanceConfig.Link] = value;
                value = context.Request.Form["MetaKeywords"];
                if (value != null) item[Alias.InstanceConfig.MetaKeywords] = value;
                value = context.Request.Form["MetaDescription"];
                if (value != null) item[Alias.InstanceConfig.MetaDescription] = value;
				value = context.Request.Form["WxppAppID"];
                if (value != null) item[Alias.InstanceConfig.WxppAppID] = value;
				value = context.Request.Form["WxppAppSecret"];
                if (value != null) item[Alias.InstanceConfig.WxppAppSecret] = value;
				value = context.Request.Form["WxmpAppID"];
				if (value != null) item[Alias.InstanceConfig.WxmpAppID] = value;
				value = context.Request.Form["WxmpAppSecret"];
				if (value != null) item[Alias.InstanceConfig.WxmpAppSecret] = value;
				value = context.Request.Form["WxAppAppID"];
                if (value != null) item[Alias.InstanceConfig.WxAppAppID] = value;
				value = context.Request.Form["WxAppAppSecret"];
                if (value != null) item[Alias.InstanceConfig.WxAppAppSecret] = value;

                SetBasicSettings(item);
                Save();
                Manager.UpdateSite("Basic", Lang);

				if (!Convert.ToBoolean(Manager.Site[SystemModule.Alias.InstanceConfig.DisableSmartUpdate]))
					Manager.ClearAllCache();
			}
            else if (operation == "SetEmailSettings")
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                string value = null;
                value = context.Request.Form["Tool"];
                if (value != null) item[Alias.InstanceConfig.ESTool] = value;
                value = context.Request.Form["Name"];
                if (value != null) item[Alias.InstanceConfig.ESName] = value;
                value = context.Request.Form["Address"];
                if (value != null) item[Alias.InstanceConfig.ESAddress] = value;
                value = context.Request.Form["Account"];
                if (value != null) item[Alias.InstanceConfig.ESAccount] = value;
                value = context.Request.Form["Password"];
                if (value != null) item[Alias.InstanceConfig.ESPassword] = value;
                value = context.Request.Form["Server"];
                if (value != null) item[Alias.InstanceConfig.ESServer] = value;
                value = context.Request.Form["NeedValidation"];
                if (value != null) item[Alias.InstanceConfig.ESNeedValidation] = value;

                SetEmailSettings(item);
                Save();
            }
            else if (operation == "SetWaterMarkSettings")
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                string value = null;
                value = context.Request.Form["Type"];
                if (value != null) item[Alias.InstanceConfig.WMSType] = value;
                value = context.Request.Form["Image"];
                if (value != null) item[Alias.InstanceConfig.WMSImage] = value;
                value = context.Request.Form["Text"];
                if (value != null) item[Alias.InstanceConfig.WMSText] = value;
                value = context.Request.Form["FontFamily"];
                if (value != null) item[Alias.InstanceConfig.WMSFontFamily] = value;
                value = context.Request.Form["FontSize"];
                if (value != null) item[Alias.InstanceConfig.WMSFontSize] = value;
                value = context.Request.Form["ForeColor"];
                if (value != null) item[Alias.InstanceConfig.WMSForeColor] = value;
                value = context.Request.Form["BackColor"];
                if (value != null) item[Alias.InstanceConfig.WMSBackColor] = value;
                value = context.Request.Form["Bold"];
                if (value != null) item[Alias.InstanceConfig.WMSBold] = value;
                value = context.Request.Form["Italic"];
                if (value != null) item[Alias.InstanceConfig.WMSItalic] = value;
                value = context.Request.Form["Position"];
                if (value != null) item[Alias.InstanceConfig.WMSPosition] = value;
                value = context.Request.Form["MarginX"];
                if (value != null) item[Alias.InstanceConfig.WMSMarginX] = value;
                value = context.Request.Form["MarginY"];
                if (value != null) item[Alias.InstanceConfig.WMSMarginY] = value;
                value = context.Request.Form["Transparency"];
                if (value != null) item[Alias.InstanceConfig.WMSTransparency] = value;

                SetWaterMarkSettings(item);
                Save();
            }
            else if (operation == "SetGlobalSettings")
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                string value = null;
                value = context.Request.Form["Domain"];
                if (value != null) item[Alias.InstanceConfig.Domain] = value;
                //value = context.Request.Form["DatabaseProvider"];
                //if (value != null) item[Alias.InstanceConfig.DatabaseProvider] = value;
                value = context.Request.Form["ProtectConnectionString"];
                if (value != null) item[Alias.InstanceConfig.ProtectConnectionString] = value;
                value = context.Request.Form["ConnectionString"];
                if (value != null) item[Alias.InstanceConfig.ConnectionString] = value;
                value = context.Request.Form["DisableSmartUpdate"];
                if (value != null) item[Alias.InstanceConfig.DisableSmartUpdate] = value;
				//value = context.Request.Form["AliCache"];
                //if (value != null) item[Alias.InstanceConfig.AliCache] = value;
                value = context.Request.Form["ClearWhiteSpace"];
                if (value != null) item[Alias.InstanceConfig.ClearWhiteSpace] = value;
                value = context.Request.Form["ClearHtmlComment"];
                if (value != null) item[Alias.InstanceConfig.ClearHtmlComment] = value;
                value = context.Request.Form["ClearScriptComment"];
                if (value != null) item[Alias.InstanceConfig.ClearScriptComment] = value;
                //value = context.Request.Form["GZipEnabled"];
                //if (value != null) item[Alias.InstanceConfig.GZipEnabled] = value;
				value = context.Request.Form["StaticFileGZipEnabled"];
				if (value != null) item[Alias.InstanceConfig.StaticFileGZipEnabled] = value;
				value = context.Request.Form["OnlyAdminCanDebug"];
                if (value != null) item[Alias.InstanceConfig.OnlyAdminCanDebug] = value;
                value = context.Request.Form["DisplayTrace"];
                if (value != null) item[Alias.InstanceConfig.DisplayTrace] = value;
                value = context.Request.Form["ShutDown"];
                if (value != null) item[Alias.InstanceConfig.ShutDown] = value;
                value = context.Request.Form["ShutDownReason"];
                if (value != null) item[Alias.InstanceConfig.ShutDownReason] = value;
                value = context.Request.Form["DataDirectory"];
                if (value != null) item[Alias.InstanceConfig.DataDirectory] = value;
                value = context.Request.Form["TemplateDirectory"];
                if (value != null) item[Alias.InstanceConfig.TemplateDirectory] = value;
                value = context.Request.Form["CacheDirectory"];
                if (value != null) item[Alias.InstanceConfig.CacheDirectory] = value;
                value = context.Request.Form["ModuleDirectory"];
                if (value != null) item[Alias.InstanceConfig.ModuleDirectory] = value;
                value = context.Request.Form["CommonDirectory"];
                if (value != null) item[Alias.InstanceConfig.CommonDirectory] = value;
                //value = context.Request.Form["Fake404"];
                //if (value != null) item[Alias.InstanceConfig.Fake404] = value;
				value = context.Request.Form["NotInWhiteListErrorCode"];
				if (value != null) item[Alias.InstanceConfig.NotInWhiteListErrorCode] = value;
				value = context.Request.Form["NotInWhiteListErrorMessage"];
				if (value != null) item[Alias.InstanceConfig.NotInWhiteListErrorMessage] = value;

                SetGlobalSettings(item);
                Manager.UpdateSite("Global", null);

				if (!Convert.ToBoolean(Manager.Site[SystemModule.Alias.InstanceConfig.DisableSmartUpdate]))
					Manager.ClearAllCache();
			}
            else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }
		public override void AliUpload(string fieldName, ref string uploadFile, bool isLast)
		{
            string operation = HttpContext.Current.Request.QueryString["Operation"];
            if (operation == "UploadLogo")
                UploadLogo(fieldName, ref uploadFile, isLast);
            else if (operation == "UploadLink")
                UploadLink(fieldName, ref uploadFile, isLast);
            else if (operation == "UploadWaterMarkImage")
                UploadWaterMarkImage(fieldName, ref uploadFile, isLast);
            else
                throw new Exception("lang_header_param");
        }

		public Dictionary<string, string> GetBasicSettings()
        {
            if (CheckRight)
            {
                ValidateRight("GetBasicSettings", Guid.Empty);
            }

            Dictionary<string, string> result = new Dictionary<string, string>();

            XmlNode basic = XmlDoc.SelectSingleNode("//BasicSettings");
            if (basic == null)
            {
                result[Alias.InstanceConfig.Name] = String.Empty;
                result[Alias.InstanceConfig.Description] = String.Empty;
                result[Alias.InstanceConfig.Notice] = String.Empty;
                result[Alias.InstanceConfig.Footer] = String.Empty;
				result[Alias.InstanceConfig.StatCode] = String.Empty;
                result[Alias.InstanceConfig.UploadType] = String.Empty;
                result[Alias.InstanceConfig.Error404Redirect] = String.Empty;
                result[Alias.InstanceConfig.ErrorRedirect] = String.Empty;
                result[Alias.InstanceConfig.DisplayCheckCode] = "false";
                result[Alias.InstanceConfig.Logo] = String.Empty;
                result[Alias.InstanceConfig.Link] = String.Empty;
                result[Alias.InstanceConfig.MetaKeywords] = String.Empty;
                result[Alias.InstanceConfig.MetaDescription] = String.Empty;
				result[Alias.InstanceConfig.WxppAppID] = String.Empty;
				result[Alias.InstanceConfig.WxppAppSecret] = String.Empty;
				result[Alias.InstanceConfig.WxmpAppID] = String.Empty;
				result[Alias.InstanceConfig.WxmpAppSecret] = String.Empty;
				result[Alias.InstanceConfig.WxAppAppID] = String.Empty;
				result[Alias.InstanceConfig.WxAppAppSecret] = String.Empty;
			}
            else
            {
                XmlNode node = null;

                node = basic.SelectSingleNode("Name");
                if (node == null)
                    result[Alias.InstanceConfig.Name] = String.Empty;
                else
                    result[Alias.InstanceConfig.Name] = node.InnerText;

                node = basic.SelectSingleNode("Description");
                if (node == null)
                    result[Alias.InstanceConfig.Description] = String.Empty;
                else
                    result[Alias.InstanceConfig.Description] = node.InnerText;

                node = basic.SelectSingleNode("Notice");
                if (node == null)
                    result[Alias.InstanceConfig.Notice] = String.Empty;
                else
                    result[Alias.InstanceConfig.Notice] = node.InnerText;

                node = basic.SelectSingleNode("Footer");
                if (node == null)
                    result[Alias.InstanceConfig.Footer] = String.Empty;
                else
                    result[Alias.InstanceConfig.Footer] = node.InnerText;

				node = basic.SelectSingleNode("StatCode");
                if (node == null)
					result[Alias.InstanceConfig.StatCode] = String.Empty;
                else
					result[Alias.InstanceConfig.StatCode] = node.InnerText;

                node = basic.SelectSingleNode("UploadType");
                if (node == null)
                    result[Alias.InstanceConfig.UploadType] = String.Empty;
                else
                    result[Alias.InstanceConfig.UploadType] = node.InnerText;

                node = basic.SelectSingleNode("Error404Redirect");
                if (node == null)
                    result[Alias.InstanceConfig.Error404Redirect] = String.Empty;
                else
                    result[Alias.InstanceConfig.Error404Redirect] = node.InnerText;

                node = basic.SelectSingleNode("ErrorRedirect");
                if (node == null)
                    result[Alias.InstanceConfig.ErrorRedirect] = String.Empty;
                else
                    result[Alias.InstanceConfig.ErrorRedirect] = node.InnerText;

                node = basic.SelectSingleNode("DisplayCheckCode");
                if (node == null)
                    result[Alias.InstanceConfig.DisplayCheckCode] = "false";
                else
                    result[Alias.InstanceConfig.DisplayCheckCode] = node.InnerText;

                node = basic.SelectSingleNode("Logo");
                if (node == null)
                    result[Alias.InstanceConfig.Logo] = String.Empty;
                else
                    result[Alias.InstanceConfig.Logo] = node.InnerText;

                node = basic.SelectSingleNode("Link");
                if (node == null)
                    result[Alias.InstanceConfig.Link] = String.Empty;
                else
                    result[Alias.InstanceConfig.Link] = node.InnerText;

                node = basic.SelectSingleNode("MetaKeywords");
                if (node == null)
                    result[Alias.InstanceConfig.MetaKeywords] = String.Empty;
                else
                    result[Alias.InstanceConfig.MetaKeywords] = node.InnerText;

                node = basic.SelectSingleNode("MetaDescription");
                if (node == null)
                    result[Alias.InstanceConfig.MetaDescription] = String.Empty;
                else
                    result[Alias.InstanceConfig.MetaDescription] = node.InnerText;

				node = basic.SelectSingleNode("WxppAppID");
				if (node == null)
					result[Alias.InstanceConfig.WxppAppID] = String.Empty;
				else
					result[Alias.InstanceConfig.WxppAppID] = node.InnerText;

				node = basic.SelectSingleNode("WxppAppSecret");
				if (node == null)
					result[Alias.InstanceConfig.WxppAppSecret] = String.Empty;
				else
					result[Alias.InstanceConfig.WxppAppSecret] = node.InnerText;

				node = basic.SelectSingleNode("WxmpAppID");
				if (node == null)
					result[Alias.InstanceConfig.WxmpAppID] = String.Empty;
				else
					result[Alias.InstanceConfig.WxmpAppID] = node.InnerText;

				node = basic.SelectSingleNode("WxmpAppSecret");
				if (node == null)
					result[Alias.InstanceConfig.WxmpAppSecret] = String.Empty;
				else
					result[Alias.InstanceConfig.WxmpAppSecret] = node.InnerText;

				node = basic.SelectSingleNode("WxAppAppID");
				if (node == null)
					result[Alias.InstanceConfig.WxAppAppID] = String.Empty;
				else
					result[Alias.InstanceConfig.WxAppAppID] = node.InnerText;

				node = basic.SelectSingleNode("WxAppAppSecret");
				if (node == null)
					result[Alias.InstanceConfig.WxAppAppSecret] = String.Empty;
				else
					result[Alias.InstanceConfig.WxAppAppSecret] = node.InnerText;
			}

            return result;
        }
		public void SetBasicSettings(Dictionary<string, string> value)
        {
            if (CheckRight)
            {
                ValidateRight("SetBasicSettings", Guid.Empty);
            }

            XmlNode node = null;
            XmlNode basic = XmlDoc.SelectSingleNode("//BasicSettings");
            if (basic == null)
            {
                basic = XmlDoc.CreateElement("BasicSettings");
                XmlDoc.DocumentElement.AppendChild(basic);
            }

            if (value.ContainsKey(Alias.InstanceConfig.Name))
            {
                node = basic.SelectSingleNode("Name");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Name");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Name]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Name];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Description))
            {
                node = basic.SelectSingleNode("Description");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Description");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Description]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Description];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Notice))
            {
                node = basic.SelectSingleNode("Notice");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Notice");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Notice]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Notice];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Footer))
            {
                node = basic.SelectSingleNode("Footer");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Footer");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Footer]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Footer];
            }

			if (value.ContainsKey(Alias.InstanceConfig.StatCode))
            {
				node = basic.SelectSingleNode("StatCode");
                if (node == null)
                {
					node = XmlDoc.CreateElement("StatCode");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.StatCode]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.StatCode];
            }

            if (value.ContainsKey(Alias.InstanceConfig.UploadType))
            {
                node = basic.SelectSingleNode("UploadType");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("UploadType");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.UploadType]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.UploadType];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Error404Redirect))
            {
                node = basic.SelectSingleNode("Error404Redirect");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Error404Redirect");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Error404Redirect]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Error404Redirect];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ErrorRedirect))
            {
                node = basic.SelectSingleNode("ErrorRedirect");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ErrorRedirect");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ErrorRedirect]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ErrorRedirect];
            }

            if (value.ContainsKey(Alias.InstanceConfig.DisplayCheckCode))
            {
                node = basic.SelectSingleNode("DisplayCheckCode");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("DisplayCheckCode");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.DisplayCheckCode]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.DisplayCheckCode];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Logo))
            {
                node = basic.SelectSingleNode("Logo");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Logo");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Logo]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Logo];
            }

            if (value.ContainsKey(Alias.InstanceConfig.Link))
            {
                node = basic.SelectSingleNode("Link");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Link");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Link]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Link];
            }

            if (value.ContainsKey(Alias.InstanceConfig.MetaKeywords))
            {
                node = basic.SelectSingleNode("MetaKeywords");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("MetaKeywords");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.MetaKeywords]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.MetaKeywords];
            }

            if (value.ContainsKey(Alias.InstanceConfig.MetaDescription))
            {
                node = basic.SelectSingleNode("MetaDescription");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("MetaDescription");
                    basic.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.MetaDescription]))
                    basic.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.MetaDescription];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxppAppID))
            {
				node = basic.SelectSingleNode("WxppAppID");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxppAppID");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxppAppID]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxppAppID];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxppAppSecret))
            {
				node = basic.SelectSingleNode("WxppAppSecret");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxppAppSecret");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxppAppSecret]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxppAppSecret];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxmpAppID))
            {
				node = basic.SelectSingleNode("WxmpAppID");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxmpAppID");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxmpAppID]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxmpAppID];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxmpAppSecret))
            {
				node = basic.SelectSingleNode("WxmpAppSecret");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxmpAppSecret");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxmpAppSecret]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxmpAppSecret];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxAppAppID))
            {
				node = basic.SelectSingleNode("WxAppAppID");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxAppAppID");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxAppAppID]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxAppAppID];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WxAppAppSecret))
            {
				node = basic.SelectSingleNode("WxAppAppSecret");
                if (node == null)
                {
					node = XmlDoc.CreateElement("WxAppAppSecret");
                    basic.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WxAppAppSecret]))
                    basic.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.WxAppAppSecret];
            }
        }

		public Dictionary<string, string> GetEmailSettings()
        {
            if (CheckRight)
            {
                ValidateRight("GetEmailSettings", Guid.Empty);
            }

            Dictionary<string, string> result = new Dictionary<string, string>();

            XmlNode email = XmlDoc.SelectSingleNode("//EmailSettings");
            if (email == null)
            {
                result[Alias.InstanceConfig.ESTool] = String.Empty;
                result[Alias.InstanceConfig.ESName] = String.Empty;
                result[Alias.InstanceConfig.ESAddress] = String.Empty;
                result[Alias.InstanceConfig.ESAccount] = String.Empty;
                result[Alias.InstanceConfig.ESPassword] = String.Empty;
                result[Alias.InstanceConfig.ESServer] = String.Empty;
                result[Alias.InstanceConfig.ESNeedValidation] = "false";
            }
            else
            {
                XmlNode node = null;

                node = email.SelectSingleNode("Tool");
                if (node == null)
                    result[Alias.InstanceConfig.ESTool] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESTool] = node.InnerText;

                node = email.SelectSingleNode("Name");
                if (node == null)
                    result[Alias.InstanceConfig.ESName] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESName] = node.InnerText;

                node = email.SelectSingleNode("Address");
                if (node == null)
                    result[Alias.InstanceConfig.ESAddress] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESAddress] = node.InnerText;

                node = email.SelectSingleNode("Account");
                if (node == null)
                    result[Alias.InstanceConfig.ESAccount] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESAccount] = node.InnerText;

                node = email.SelectSingleNode("Password");
                if (node == null)
                    result[Alias.InstanceConfig.ESPassword] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESPassword] = Decrypt(node.InnerText);

                node = email.SelectSingleNode("Server");
                if (node == null)
                    result[Alias.InstanceConfig.ESServer] = String.Empty;
                else
                    result[Alias.InstanceConfig.ESServer] = node.InnerText;

                node = email.SelectSingleNode("NeedValidation");
                if (node == null)
                    result[Alias.InstanceConfig.ESNeedValidation] = "false";
                else
                    result[Alias.InstanceConfig.ESNeedValidation] = node.InnerText;
            }

            return result;
        }
		public void SetEmailSettings(Dictionary<string, string> value)
        {
            if (CheckRight)
            {
                ValidateRight("SetEmailSettings", Guid.Empty);
            }

            XmlNode node = null;
            XmlNode email = XmlDoc.SelectSingleNode("//EmailSettings");
            if (email == null)
            {
                email = XmlDoc.CreateElement("EmailSettings");
                XmlDoc.DocumentElement.AppendChild(email);
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESTool))
            {
                node = email.SelectSingleNode("Tool");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Tool");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESTool]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESTool];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESName))
            {
                node = email.SelectSingleNode("Name");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Name");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESName]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESName];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESAddress))
            {
                node = email.SelectSingleNode("Address");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Address");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESAddress]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESAddress];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESAccount))
            {
                node = email.SelectSingleNode("Account");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Account");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESAccount]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESAccount];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESPassword))
            {
                node = email.SelectSingleNode("Password");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Password");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESPassword]))
                    email.RemoveChild(node);
                else
                    node.InnerText = Encrypt(value[Alias.InstanceConfig.ESPassword]);
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESServer))
            {
                node = email.SelectSingleNode("Server");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Server");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESServer]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESServer];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ESNeedValidation))
            {
                node = email.SelectSingleNode("NeedValidation");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("NeedValidation");
                    email.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ESNeedValidation]))
                    email.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ESNeedValidation];
            }
        }

		public Dictionary<string, string> GetWaterMarkSettings()
        {
            if (CheckRight)
            {
                ValidateRight("GetWaterMarkSettings", Guid.Empty);
            }

            Dictionary<string, string> result = new Dictionary<string, string>();

            XmlNode mark = XmlDoc.SelectSingleNode("//WaterMarkSettings");
            if (mark == null)
            {
                result[Alias.InstanceConfig.WMSType] = "Text";
                result[Alias.InstanceConfig.WMSImage] = String.Empty;
                result[Alias.InstanceConfig.WMSText] = String.Empty;
                result[Alias.InstanceConfig.WMSFontFamily] = "Arial";
                result[Alias.InstanceConfig.WMSFontSize] = "10";
                result[Alias.InstanceConfig.WMSForeColor] = "#000000";
                result[Alias.InstanceConfig.WMSBackColor] = String.Empty;
                result[Alias.InstanceConfig.WMSBold] = "false";
                result[Alias.InstanceConfig.WMSItalic] = "false";
                result[Alias.InstanceConfig.WMSPosition] = "TopLeft";
                result[Alias.InstanceConfig.WMSMarginX] = "0";
                result[Alias.InstanceConfig.WMSMarginY] = "0";
                result[Alias.InstanceConfig.WMSTransparency] = "0.5";
            }
            else
            {
                XmlNode node = null;

                node = mark.SelectSingleNode("Type");
                if (node == null)
                    result[Alias.InstanceConfig.WMSType] = "Text";
                else
                    result[Alias.InstanceConfig.WMSType] = node.InnerText;

                node = mark.SelectSingleNode("Image");
                if (node == null)
                    result[Alias.InstanceConfig.WMSImage] = String.Empty;
                else
                    result[Alias.InstanceConfig.WMSImage] = node.InnerText;

                node = mark.SelectSingleNode("Text");
                if (node == null)
                    result[Alias.InstanceConfig.WMSText] = String.Empty;
                else
                    result[Alias.InstanceConfig.WMSText] = node.InnerText;

                node = mark.SelectSingleNode("FontFamily");
                if (node == null)
                    result[Alias.InstanceConfig.WMSFontFamily] = "Arial";
                else
                    result[Alias.InstanceConfig.WMSFontFamily] = node.InnerText;

                node = mark.SelectSingleNode("FontSize");
                if (node == null)
                    result[Alias.InstanceConfig.WMSFontSize] = "10";
                else
                    result[Alias.InstanceConfig.WMSFontSize] = node.InnerText;

                node = mark.SelectSingleNode("ForeColor");
                if (node == null)
                    result[Alias.InstanceConfig.WMSForeColor] = "#000000";
                else
                    result[Alias.InstanceConfig.WMSForeColor] = node.InnerText;

                node = mark.SelectSingleNode("BackColor");
                if (node == null)
                    result[Alias.InstanceConfig.WMSBackColor] = String.Empty;
                else
                    result[Alias.InstanceConfig.WMSBackColor] = node.InnerText;

                node = mark.SelectSingleNode("Bold");
                if (node == null)
                    result[Alias.InstanceConfig.WMSBold] = "false";
                else
                    result[Alias.InstanceConfig.WMSBold] = node.InnerText;

                node = mark.SelectSingleNode("Italic");
                if (node == null)
                    result[Alias.InstanceConfig.WMSItalic] = "false";
                else
                    result[Alias.InstanceConfig.WMSItalic] = node.InnerText;

                node = mark.SelectSingleNode("Position");
                if (node == null)
                    result[Alias.InstanceConfig.WMSPosition] = "TopLeft";
                else
                    result[Alias.InstanceConfig.WMSPosition] = node.InnerText;

                node = mark.SelectSingleNode("MarginX");
                if (node == null)
                    result[Alias.InstanceConfig.WMSMarginX] = "0";
                else
                    result[Alias.InstanceConfig.WMSMarginX] = node.InnerText;

                node = mark.SelectSingleNode("MarginY");
                if (node == null)
                    result[Alias.InstanceConfig.WMSMarginY] = "0";
                else
                    result[Alias.InstanceConfig.WMSMarginY] = node.InnerText;

                node = mark.SelectSingleNode("Transparency");
                if (node == null)
                    result[Alias.InstanceConfig.WMSTransparency] = "0.5";
                else
                    result[Alias.InstanceConfig.WMSTransparency] = node.InnerText;
            }

            return result;
        }
		public void SetWaterMarkSettings(Dictionary<string, string> value)
        {
            if (CheckRight)
            {
                ValidateRight("SetWaterMarkSettings", Guid.Empty);
            }

            XmlNode node = null;
            XmlNode mark = XmlDoc.SelectSingleNode("//WaterMarkSettings");
            if (mark == null)
            {
                mark = XmlDoc.CreateElement("WaterMarkSettings");
                XmlDoc.DocumentElement.AppendChild(mark);
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSType))
            {
                node = mark.SelectSingleNode("Type");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Type");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSType]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSType];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSImage))
            {
                node = mark.SelectSingleNode("Image");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Image");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSImage]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSImage];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSText))
            {
                node = mark.SelectSingleNode("Text");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Text");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSText]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSText];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSFontFamily))
            {
                node = mark.SelectSingleNode("FontFamily");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("FontFamily");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSFontFamily]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSFontFamily];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSFontSize))
            {
                node = mark.SelectSingleNode("FontSize");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("FontSize");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSFontSize]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSFontSize];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSForeColor))
            {
                node = mark.SelectSingleNode("ForeColor");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ForeColor");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSForeColor]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSForeColor];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSBackColor))
            {
                node = mark.SelectSingleNode("BackColor");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("BackColor");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSBackColor]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSBackColor];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSBold))
            {
                node = mark.SelectSingleNode("Bold");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Bold");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSBold]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSBold];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSItalic))
            {
                node = mark.SelectSingleNode("Italic");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Italic");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSItalic]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSItalic];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSPosition))
            {
                node = mark.SelectSingleNode("Position");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Position");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSPosition]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSPosition];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSMarginX))
            {
                node = mark.SelectSingleNode("MarginX");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("MarginX");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSMarginX]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSMarginX];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSMarginY))
            {
                node = mark.SelectSingleNode("MarginY");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("MarginY");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSMarginY]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSMarginY];
            }

            if (value.ContainsKey(Alias.InstanceConfig.WMSTransparency))
            {
                node = mark.SelectSingleNode("Transparency");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Transparency");
                    mark.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.WMSTransparency]))
                    mark.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.WMSTransparency];
            }
        }

		public Dictionary<string, string> GetGlobalSettings()
        {
            if (CheckRight)
            {
                ValidateRight("GetGlobalSettings", Guid.Empty);
            }

            string path = Manager.MapPath(Manager.GetWebPath() + "alicms.config");
            if (!File.Exists(path))
                throw new Exception("lang_alicms_config_not_exist");

            Dictionary<string, string> result = new Dictionary<string, string>();

            XmlDoc.Load(path);

            XmlNode global = XmlDoc.SelectSingleNode("//GlobalSettings");
            if (global == null)
                throw new Exception("lang_alicms_config_not_correct");

            XmlNode node = null;

            node = global.SelectSingleNode("SystemVersion");
            if (node == null)
                throw new Exception("lang_alicms_config_must_have_version");
			result[Alias.InstanceConfig.SystemVersion] = Decrypt(node.InnerText);

			node = global.SelectSingleNode("Version");
			if (node == null)
				result[Alias.InstanceConfig.Version] = "";
			else
				result[Alias.InstanceConfig.Version] = node.InnerText;

			node = global.SelectSingleNode("ProtectConnectionString");
            if (node == null)
                result[Alias.InstanceConfig.ProtectConnectionString] = "false";
            else
                result[Alias.InstanceConfig.ProtectConnectionString] = node.InnerText;

            node = global.SelectSingleNode("ConnectionString");
            if (node == null)
                throw new Exception("lang_alicms_config_must_have_connection_string");

            if (Convert.ToBoolean(result[Alias.InstanceConfig.ProtectConnectionString]))
                result[Alias.InstanceConfig.ConnectionString] = Decrypt(node.InnerText);
            else
                result[Alias.InstanceConfig.ConnectionString] = node.InnerText;

            node = global.SelectSingleNode("DisableSmartUpdate");
            if (node == null)
                result[Alias.InstanceConfig.DisableSmartUpdate] = "false";
            else
                result[Alias.InstanceConfig.DisableSmartUpdate] = node.InnerText;

			/*
			node = global.SelectSingleNode("AliCache");
            if (node == null)
                result[Alias.InstanceConfig.AliCache] = String.Empty;
            else
				result[Alias.InstanceConfig.AliCache] = node.InnerText;
			*/

            node = global.SelectSingleNode("Domain");
            if (node == null)
                result[Alias.InstanceConfig.Domain] = "http://" + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.IsDefaultPort ? "/" : ":" + HttpContext.Current.Request.Url.Port + "/");
            else
                result[Alias.InstanceConfig.Domain] = node.InnerText;

            node = global.SelectSingleNode("DatabaseProvider");
            if (node == null)
                result[Alias.InstanceConfig.DatabaseProvider] = "OleDb";
            else
                result[Alias.InstanceConfig.DatabaseProvider] = node.InnerText;

            node = global.SelectSingleNode("ClearWhiteSpace");
            if (node == null)
                result[Alias.InstanceConfig.ClearWhiteSpace] = "true";
            else
                result[Alias.InstanceConfig.ClearWhiteSpace] = node.InnerText;

            node = global.SelectSingleNode("ClearHtmlComment");
            if (node == null)
                result[Alias.InstanceConfig.ClearHtmlComment] = "true";
            else
                result[Alias.InstanceConfig.ClearHtmlComment] = node.InnerText;

            node = global.SelectSingleNode("ClearScriptComment");
            if (node == null)
                result[Alias.InstanceConfig.ClearScriptComment] = "true";
            else
                result[Alias.InstanceConfig.ClearScriptComment] = node.InnerText;

			/*
            node = global.SelectSingleNode("GZipEnabled");
            if (node == null)
                result[Alias.InstanceConfig.GZipEnabled] = "true";
            else
                result[Alias.InstanceConfig.GZipEnabled] = node.InnerText;
			*/

			node = global.SelectSingleNode("StaticFileGZipEnabled");
			if (node == null)
				result[Alias.InstanceConfig.StaticFileGZipEnabled] = "true";
			else
				result[Alias.InstanceConfig.StaticFileGZipEnabled] = node.InnerText;

            node = global.SelectSingleNode("OnlyAdminCanDebug");
            if (node == null)
                result[Alias.InstanceConfig.OnlyAdminCanDebug] = "false";
            else
                result[Alias.InstanceConfig.OnlyAdminCanDebug] = node.InnerText;

            node = global.SelectSingleNode("DisplayTrace");
            if (node == null)
                result[Alias.InstanceConfig.DisplayTrace] = "false";
            else
                result[Alias.InstanceConfig.DisplayTrace] = node.InnerText;

            node = global.SelectSingleNode("ShutDown");
            if (node == null)
                result[Alias.InstanceConfig.ShutDown] = "false";
            else
                result[Alias.InstanceConfig.ShutDown] = node.InnerText;

            node = global.SelectSingleNode("ShutDownReason");
            if (node == null)
                result[Alias.InstanceConfig.ShutDownReason] = String.Empty;
            else
                result[Alias.InstanceConfig.ShutDownReason] = node.InnerText;

            node = global.SelectSingleNode("DataDirectory");
            if (node == null)
                result[Alias.InstanceConfig.DataDirectory] = "data";
            else
                result[Alias.InstanceConfig.DataDirectory] = node.InnerText;

            node = global.SelectSingleNode("TemplateDirectory");
            if (node == null)
                result[Alias.InstanceConfig.TemplateDirectory] = "template";
            else
                result[Alias.InstanceConfig.TemplateDirectory] = node.InnerText;

            node = global.SelectSingleNode("CacheDirectory");
            if (node == null)
                result[Alias.InstanceConfig.CacheDirectory] = "cache";
            else
                result[Alias.InstanceConfig.CacheDirectory] = node.InnerText;

            node = global.SelectSingleNode("ModuleDirectory");
            if (node == null)
                result[Alias.InstanceConfig.ModuleDirectory] = "module";
            else
                result[Alias.InstanceConfig.ModuleDirectory] = node.InnerText;

            node = global.SelectSingleNode("CommonDirectory");
            if (node == null)
                result[Alias.InstanceConfig.CommonDirectory] = "common";
            else
                result[Alias.InstanceConfig.CommonDirectory] = node.InnerText;

			/*
            node = global.SelectSingleNode("Fake404");
            if (node == null)
                result[Alias.InstanceConfig.Fake404] = "false";
            else
                result[Alias.InstanceConfig.Fake404] = node.InnerText;
			*/

			node = global.SelectSingleNode("NotInWhiteListErrorCode");
            if (node == null)
				result[Alias.InstanceConfig.NotInWhiteListErrorCode] = "200";
            else
				result[Alias.InstanceConfig.NotInWhiteListErrorCode] = node.InnerText;

			node = global.SelectSingleNode("NotInWhiteListErrorMessage");
            if (node == null)
				result[Alias.InstanceConfig.NotInWhiteListErrorMessage] = "";
            else
				result[Alias.InstanceConfig.NotInWhiteListErrorMessage] = node.InnerText;

            return result;
        }
		public void SetGlobalSettings(Dictionary<string, string> value)
        {
            if (CheckRight)
            {
                ValidateRight("SetGlobalSettings", Guid.Empty);
            }

            string path = Manager.MapPath(Manager.GetWebPath() + "alicms.config");
            if (!File.Exists(path))
                throw new Exception("lang_alicms_config_not_exist");

            XmlDoc.Load(path);

            XmlNode node = null;
            XmlNode global = XmlDoc.SelectSingleNode("//GlobalSettings");
            if (global == null)
            {
                global = XmlDoc.CreateElement("GlobalSettings");
                XmlDoc.DocumentElement.AppendChild(global);
            }

            bool protectConnectionString = false;
            if (value.ContainsKey(Alias.InstanceConfig.ProtectConnectionString))
            {
                node = global.SelectSingleNode("ProtectConnectionString");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ProtectConnectionString");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ProtectConnectionString]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    node.InnerText = value[Alias.InstanceConfig.ProtectConnectionString];
                    protectConnectionString = Convert.ToBoolean(node.InnerText);
                }
            }

            if (value.ContainsKey(Alias.InstanceConfig.ConnectionString))
            {
                node = global.SelectSingleNode("ConnectionString");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ConnectionString");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ConnectionString]))
                    throw new Exception("lang_connection_string_must_set");

                if (protectConnectionString)
                    node.InnerText = Encrypt(value[Alias.InstanceConfig.ConnectionString]);
                else
                    node.InnerText = value[Alias.InstanceConfig.ConnectionString];
            }

            if (value.ContainsKey(Alias.InstanceConfig.DisableSmartUpdate))
            {
                node = global.SelectSingleNode("DisableSmartUpdate");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("DisableSmartUpdate");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.DisableSmartUpdate]) || !Convert.ToBoolean(value[Alias.InstanceConfig.DisableSmartUpdate]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.DisableSmartUpdate];
            }

			/*
			if (value.ContainsKey(Alias.InstanceConfig.AliCache))
            {
				node = global.SelectSingleNode("AliCache");
                if (node == null)
                {
					node = XmlDoc.CreateElement("AliCache");
                    global.AppendChild(node);
                }
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.AliCache]))
                    global.RemoveChild(node);
                else
					node.InnerText = value[Alias.InstanceConfig.AliCache];
            }
			*/

            if (value.ContainsKey(Alias.InstanceConfig.Domain))
            {
                node = global.SelectSingleNode("Domain");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Domain");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Domain]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Domain];
            }

			/*
            if (value.ContainsKey(Alias.InstanceConfig.DatabaseProvider))
            {
                node = global.SelectSingleNode("DatabaseProvider");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("DatabaseProvider");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.DatabaseProvider]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.DatabaseProvider];
            }
			*/

            if (value.ContainsKey(Alias.InstanceConfig.ClearWhiteSpace))
            {
                node = global.SelectSingleNode("ClearWhiteSpace");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ClearWhiteSpace");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ClearWhiteSpace]) || Convert.ToBoolean(value[Alias.InstanceConfig.ClearWhiteSpace]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ClearWhiteSpace];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ClearHtmlComment))
            {
                node = global.SelectSingleNode("ClearHtmlComment");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ClearHtmlComment");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ClearHtmlComment]) || Convert.ToBoolean(value[Alias.InstanceConfig.ClearHtmlComment]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ClearHtmlComment];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ClearScriptComment))
            {
                node = global.SelectSingleNode("ClearScriptComment");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ClearScriptComment");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ClearScriptComment]) || Convert.ToBoolean(value[Alias.InstanceConfig.ClearScriptComment]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ClearScriptComment];
            }

			/*
            if (value.ContainsKey(Alias.InstanceConfig.GZipEnabled))
            {
                node = global.SelectSingleNode("GZipEnabled");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("GZipEnabled");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.GZipEnabled]) || Convert.ToBoolean(value[Alias.InstanceConfig.GZipEnabled]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.GZipEnabled];
            }
			*/

			if (value.ContainsKey(Alias.InstanceConfig.StaticFileGZipEnabled))
			{
				node = global.SelectSingleNode("StaticFileGZipEnabled");
				if (node == null)
				{
					node = XmlDoc.CreateElement("StaticFileGZipEnabled");
					global.AppendChild(node);
				}
				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.StaticFileGZipEnabled]) || Convert.ToBoolean(value[Alias.InstanceConfig.StaticFileGZipEnabled]))
					global.RemoveChild(node);
				else
					node.InnerText = value[Alias.InstanceConfig.StaticFileGZipEnabled];
			}

            if (value.ContainsKey(Alias.InstanceConfig.OnlyAdminCanDebug))
            {
                node = global.SelectSingleNode("OnlyAdminCanDebug");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("OnlyAdminCanDebug");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.OnlyAdminCanDebug]) || !Convert.ToBoolean(value[Alias.InstanceConfig.OnlyAdminCanDebug]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.OnlyAdminCanDebug];
            }

            if (value.ContainsKey(Alias.InstanceConfig.DisplayTrace))
            {
                node = global.SelectSingleNode("DisplayTrace");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("DisplayTrace");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.DisplayTrace]) || !Convert.ToBoolean(value[Alias.InstanceConfig.DisplayTrace]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.DisplayTrace];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ShutDown))
            {
                node = global.SelectSingleNode("ShutDown");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ShutDown");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ShutDown]) || !Convert.ToBoolean(value[Alias.InstanceConfig.ShutDown]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ShutDown];
            }

            if (value.ContainsKey(Alias.InstanceConfig.ShutDownReason))
            {
                node = global.SelectSingleNode("ShutDownReason");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ShutDownReason");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ShutDownReason]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.ShutDownReason];
            }

            if (value.ContainsKey(Alias.InstanceConfig.DataDirectory))
            {
                node = global.SelectSingleNode("DataDirectory");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("DataDirectory");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.DataDirectory]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    string oldPath = Manager.MapPath(Manager.GetWebPath() + node.InnerText);
                    string newPath = Manager.MapPath(Manager.GetWebPath() + value[Alias.InstanceConfig.DataDirectory]);

                    if (!String.IsNullOrEmpty(node.InnerText) && oldPath != newPath)
                        Directory.Move(oldPath, newPath);

                    node.InnerText = value[Alias.InstanceConfig.DataDirectory];
                }
            }

            if (value.ContainsKey(Alias.InstanceConfig.TemplateDirectory))
            {
                node = global.SelectSingleNode("TemplateDirectory");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("TemplateDirectory");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.TemplateDirectory]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    string oldPath = Manager.MapPath(Manager.GetWebPath() + node.InnerText);
                    string newPath = Manager.MapPath(Manager.GetWebPath() + value[Alias.InstanceConfig.TemplateDirectory]);

                    if (!String.IsNullOrEmpty(node.InnerText) && oldPath != newPath)
                        Directory.Move(oldPath, newPath);

                    node.InnerText = value[Alias.InstanceConfig.TemplateDirectory];
                }
            }

            if (value.ContainsKey(Alias.InstanceConfig.CacheDirectory))
            {
                node = global.SelectSingleNode("CacheDirectory");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("CacheDirectory");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.CacheDirectory]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    string oldPath = Manager.MapPath(Manager.GetWebPath() + node.InnerText);
                    string newPath = Manager.MapPath(Manager.GetWebPath() + value[Alias.InstanceConfig.CacheDirectory]);

                    if (!String.IsNullOrEmpty(node.InnerText) && oldPath != newPath)
                        Manager.DeleteFolder(oldPath, true);

                    node.InnerText = value[Alias.InstanceConfig.CacheDirectory];
                }
            }

            if (value.ContainsKey(Alias.InstanceConfig.ModuleDirectory))
            {
                node = global.SelectSingleNode("ModuleDirectory");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("ModuleDirectory");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.ModuleDirectory]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    string oldPath = Manager.MapPath(Manager.GetWebPath() + node.InnerText);
                    string newPath = Manager.MapPath(Manager.GetWebPath() + value[Alias.InstanceConfig.ModuleDirectory]);

                    if (!String.IsNullOrEmpty(node.InnerText) && oldPath != newPath)
                        Directory.Move(oldPath, newPath);

                    node.InnerText = value[Alias.InstanceConfig.ModuleDirectory];
                }
            }

            if (value.ContainsKey(Alias.InstanceConfig.CommonDirectory))
            {
                node = global.SelectSingleNode("CommonDirectory");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("CommonDirectory");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.CommonDirectory]))
                {
                    global.RemoveChild(node);
                }
                else
                {
                    string oldPath = Manager.MapPath(Manager.GetWebPath() + node.InnerText);
                    string newPath = Manager.MapPath(Manager.GetWebPath() + value[Alias.InstanceConfig.CommonDirectory]);

                    if (!String.IsNullOrEmpty(node.InnerText) && oldPath != newPath)
                        Directory.Move(oldPath, newPath);

                    node.InnerText = value[Alias.InstanceConfig.CommonDirectory];
                }
            }

			/*
            if (value.ContainsKey(Alias.InstanceConfig.Fake404))
            {
                node = global.SelectSingleNode("Fake404");
                if (node == null)
                {
                    node = XmlDoc.CreateElement("Fake404");
                    global.AppendChild(node);
                }
                if (String.IsNullOrEmpty(value[Alias.InstanceConfig.Fake404]) || !Convert.ToBoolean(value[Alias.InstanceConfig.Fake404]))
                    global.RemoveChild(node);
                else
                    node.InnerText = value[Alias.InstanceConfig.Fake404];
            }
			*/

			if (value.ContainsKey(Alias.InstanceConfig.NotInWhiteListErrorCode))
			{
				node = global.SelectSingleNode("NotInWhiteListErrorCode");
				if (node == null)
				{
					node = XmlDoc.CreateElement("NotInWhiteListErrorCode");
					global.AppendChild(node);
				}

				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.NotInWhiteListErrorCode]) || value[Alias.InstanceConfig.NotInWhiteListErrorCode] == "200")
					global.RemoveChild(node);
				else
					node.InnerText = value[Alias.InstanceConfig.NotInWhiteListErrorCode];
			}

			if (value.ContainsKey(Alias.InstanceConfig.NotInWhiteListErrorMessage))
			{
				node = global.SelectSingleNode("NotInWhiteListErrorMessage");
				if (node == null)
				{
					node = XmlDoc.CreateElement("NotInWhiteListErrorMessage");
					global.AppendChild(node);
				}

				if (String.IsNullOrEmpty(value[Alias.InstanceConfig.NotInWhiteListErrorMessage]))
					global.RemoveChild(node);
				else
					node.InnerText = value[Alias.InstanceConfig.NotInWhiteListErrorMessage];
			}

            XmlDoc.Save(path);
        }

		public DataTable GetLanguageSettings()
        {
            if (CheckRight)
            {
                ValidateRight("GetLanguageSettings", Guid.Empty);
            }

            string path = Manager.MapPath(Manager.GetWebPath() + "alicms.config");
            if (!File.Exists(path))
                throw new Exception("lang_alicms_config_not_exist");

            DataTable result = new DataTable();
            result.Columns.Add(Alias.Language.ID, typeof(Guid));
            result.Columns.Add(Alias.Language.InnerID, typeof(int));
            result.Columns.Add(Alias.Language.Name, typeof(string));
            result.Columns.Add(Alias.Language.Ab, typeof(string));
            result.Columns.Add(Alias.Language.Direction, typeof(string));
            result.Columns.Add(Alias.Language.FontFamily, typeof(string));
            result.Columns.Add(Alias.Language.Eot, typeof(string));

            XmlDoc.Load(path);

            XmlNodeList nodes = XmlDoc.SelectNodes("//LanguageSettings/Language");
            foreach (XmlNode node in nodes)
            {
                Guid id = new Guid(node.SelectSingleNode("ID").InnerText);

                XmlNode n = null;
                if (!String.IsNullOrEmpty(Lang))
                    n = node.SelectSingleNode("Name/" + Lang);
                if (n == null)
                    n = node.SelectSingleNode("Name/Default");
                string name = n.InnerText;
                string innerID = node.SelectSingleNode("InnerID").InnerText;
                string ab = node.SelectSingleNode("Ab").InnerText;
                string direction = node.SelectSingleNode("Direction").InnerText;
                
                string fontFamily = String.Empty;
                n = node.SelectSingleNode("FontFamily");
                if (n != null)
                    fontFamily = n.InnerText;

                string eot = String.Empty;
                n = node.SelectSingleNode("Eot");
                if (n != null)
                    eot = n.InnerText;

                DataRow newRow = result.NewRow();
                newRow[Alias.Language.ID] = id;
                newRow[Alias.Language.InnerID] = innerID;
                newRow[Alias.Language.Name] = name;
                newRow[Alias.Language.Ab] = ab;
                newRow[Alias.Language.Direction] = direction;
                newRow[Alias.Language.FontFamily] = fontFamily;
                newRow[Alias.Language.Eot] = eot;
                result.Rows.Add(newRow);
            }

            return result;
        }

		public void UploadLogo(string fieldName, ref string uploadFile, bool isLast)
        {
            if (CheckRight)
            {
                ValidateRight("UploadLogo", Guid.Empty);
            }

            if (!isLast)
            {
                string path = Manager.GetDataPath(InstanceID, Lang);

                if (Convert.ToBoolean(HttpContext.Current.Request.QueryString["UploadFileNameBySystem"]))
                    uploadFile = path + "logo" + Path.GetExtension(uploadFile);
                else
                    uploadFile = path + uploadFile;
            }
            else
            {
                XmlNode basic = XmlDoc.SelectSingleNode("//BasicSettings");
                if (basic == null)
                {
                    basic = XmlDoc.CreateElement("BasicSettings");
                    XmlDoc.DocumentElement.AppendChild(basic);
                }

                XmlNode logo = basic.SelectSingleNode("Logo");
                if (logo == null)
                {
                    logo = XmlDoc.CreateElement("Logo");
                    basic.AppendChild(logo);
                }

                logo.InnerText = uploadFile;

                Save();
                Manager.UpdateSite("Basic", Lang);

				if (!Convert.ToBoolean(Manager.Site[SystemModule.Alias.InstanceConfig.DisableSmartUpdate]))
					Manager.ClearAllCache();
			}
        }
		public void UploadLink(string fieldName, ref string uploadFile, bool isLast)
        {
            if (CheckRight)
            {
                ValidateRight("UploadLink", Guid.Empty);
            }

            if (!isLast)
            {
                string path = Manager.GetDataPath(InstanceID, Lang);

                if (Convert.ToBoolean(HttpContext.Current.Request.QueryString["UploadFileNameBySystem"]))
                    uploadFile = path + "link" + Path.GetExtension(uploadFile);
                else
                    uploadFile = path + uploadFile;
            }
            else
            {
                XmlNode basic = XmlDoc.SelectSingleNode("//BasicSettings");
                if (basic == null)
                {
                    basic = XmlDoc.CreateElement("BasicSettings");
                    XmlDoc.DocumentElement.AppendChild(basic);
                }

                XmlNode link = basic.SelectSingleNode("Link");
                if (link == null)
                {
                    link = XmlDoc.CreateElement("Link");
                    basic.AppendChild(link);
                }

                link.InnerText = uploadFile;

                Save();
                Manager.UpdateSite("Basic", Lang);

				if (!Convert.ToBoolean(Manager.Site[SystemModule.Alias.InstanceConfig.DisableSmartUpdate]))
					Manager.ClearAllCache();
			}
        }
		public void UploadWaterMarkImage(string fieldName, ref string uploadFile, bool isLast)
        {
            if (CheckRight)
            {
                ValidateRight("UploadWaterMark", Guid.Empty);
            }

            if (!isLast)
            {
                string path = Manager.GetDataPath(InstanceID, Lang);

                if (Convert.ToBoolean(HttpContext.Current.Request.QueryString["UploadFileNameBySystem"]))
                    uploadFile = path + "watermark" + Path.GetExtension(uploadFile);
                else
                    uploadFile = path + uploadFile;
            }
            else
            {
                XmlNode mark = XmlDoc.SelectSingleNode("//WaterMarkSettings");
                if (mark == null)
                {
                    mark = XmlDoc.CreateElement("WaterMarkSettings");
                    XmlDoc.DocumentElement.AppendChild(mark);
                }

                XmlNode image = mark.SelectSingleNode("Image");
                if (image == null)
                {
                    image = XmlDoc.CreateElement("Image");
                    mark.AppendChild(image);
                }

                image.InnerText = uploadFile;

                Save();
            }
        }

		public void Save()
		{
            string path = Manager.MapPath(Manager.GetDataPath(InstanceID, Lang));
            Manager.MakeSureFolderExist(path);

            XmlDoc.Save(path + "instance.config");
		}

		public string Encrypt(string src)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cs, Encoding.UTF8);
            sw.Write(src);
            sw.Close();

            string result = Convert.ToBase64String(des.Key).Replace('=', '$') + '!' + Convert.ToBase64String(des.IV).Replace('=', '@') + '#' + Convert.ToBase64String(ms.ToArray());

            cs.Close();
            ms.Close();
            des.Clear();

            return result;
        }
		public string Decrypt(string src)
        {
            string result = String.Empty;

            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                int start = src.IndexOf('!');
                byte[] key = Convert.FromBase64String(src.Substring(0, start).Replace('$', '='));

                int end = src.IndexOf('#', start + 1);
                byte[] iv = Convert.FromBase64String(src.Substring(start + 1, end - start - 1).Replace('@', '='));

                MemoryStream ms = new MemoryStream(Convert.FromBase64String(src.Substring(end + 1)));
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs, Encoding.UTF8);

                result = sr.ReadToEnd();

                sr.Close();
                cs.Close();
                ms.Close();
                des.Clear();
            }
            catch
            {
            }

            return result;
        }
	}
}
